package pl.com.qapps.datastore.berkeley;

import java.io.File;
import java.io.IOException;
import java.util.Map.Entry;

import pl.com.qapps.datastore.BytesHelper;
import pl.com.qapps.datastore.GaeHelper;
import pl.com.qapps.datastore.berkeley.impl.RangeScan;


import com.google.appengine.api.datastore.Entity;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Transaction;

public class StoreTest {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		GaeHelper.registerEnvironment();

		final Store ds = new Store(new File("./datastore"));
		ArtificialEnv env = new ArtificialEnv() {

			// final EntityValueBinding bind = new EntityValueBinding(
			// ds.getClassCatalog());

			@Override
			public void setUp(Store ds) throws DatabaseException {
//				ds.getEntityDb().
				long id = -1;
				for (int i = 0; i < 10; i++) {
					++id;
					Entity entity = new Entity("Person", id + 1);
					entity.setProperty("name", "kuba");
					// ds.getKind().put(entity.getKind(), entity);
//					ds.getEntity().put(entity.getKey(), entity);
					// DatabaseEntry key = new DatabaseEntry();
					// bind.objectToKey(entity, key);
					// DatabaseEntry data = new DatabaseEntry();
					// bind.objectToData(entity, data);
					// ds.getEntityDb().put(null, key, data);
				}
//				for (int i = 0; i < 10; i++) {
//					++id;
//					Entity entity = new Entity("Person", id + 1);
//					entity.setProperty("name", "kub");
//					ds.getEntity().put(entity.getKey(), entity);
//				}
//				for (int i = 0; i < 10; i++) {
//					++id;
//					Entity entity = new Entity("Person", id + 1);
//					entity.setProperty("name", "luba");
//					ds.getEntity().put(entity.getKey(), entity);
//				}
//				for (int i = 0; i < 10; i++) {
//					++id;
//					Entity entity = new Entity("Person", id + 1);
//					entity.setProperty("name", "auba");
//					ds.getEntity().put(entity.getKey(), entity);
//				}
//				for (int i = 0; i < 10; i++) {
//					++id;
//					Entity entity = new Entity("Person", id + 1);
//					entity.setProperty("name", "z");
//					ds.getEntity().put(entity.getKey(), entity);
//				}

			}

			@Override
			public void cleanUp(Store ds) throws DatabaseException {
//				ds.getEntity().clear();
			}

		}.init(ds);

		Transaction txn = null;
		DatabaseEntry first = new SinglePropertyKey("Person", "name",
				BytesHelper.serialize("k"));
		// DatabaseEntry last = null;
		DatabaseEntry last = new SinglePropertyKey("Person", "name",
				BytesHelper.serialize("luba"));
		boolean firstIncl = true;
		boolean lastIncl = false;
		boolean sortDup = true;
		// range scan
		RangeScan scan = new RangeScan(first, last, firstIncl, lastIncl,
				sortDup, 1L) {

			@Override
			protected boolean nextMatch(DatabaseEntry key, DatabaseEntry data) {
//				System.out.println("matched: "
//						+ ds.getEntityValueBinding().entryToObject(key, data));
				return true;
			}

		};

		scan.scan(ds.getAscDb(), txn);

		// print(ds.getEntity().values());
		// print(ds.getKind().values());
		// print(ds.getKind().duplicates("Person"));

		// DatabaseEntry data = new DatabaseEntry(BytesHelper.serialize(1L));
		// System.out.println(ds.getIdDb().put(null,
		// new DatabaseEntry(new byte[] { 0 }), data));
		// DatabaseEntry result = new DatabaseEntry();
		// ds.getIdDb().get(null, new DatabaseEntry(new byte[] { 0 }), data,
		// LockMode.DEFAULT);
		// System.out.println("result: " + Arrays.toString(data.getData()));

		env.close(ds);
		ds.close();

	}

	public static void print(Iterable<Entity> entities) {
		System.out.println(" -------------------->");
		int count = 0;
		for (Entity entity : entities) {
			System.out.println("	" + entity.getKey());
			for (Entry<String, Object> property : entity.getProperties()
					.entrySet()) {
				System.out.println("		" + property.getKey() + "="
						+ property.getValue());
			}
			count++;
		}
		System.out.println("total amount: " + count);
	}

}
